/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*	
*	Julio Montes e-mail: imc.coder@gmail.com 
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/


#include "sdk/include/pluginsmgrdlg.h"
#include "sdk/include/path.h"
#include "sdk/include/filecfg.h"

#include <wx/dir.h>

#define ID_ENABLE 3001
#define ID_DISABLE 3002

namespace sdk
{
    BEGIN_EVENT_TABLE( PluginsManagerDialog, wxDialog )
        EVT_LIST_ITEM_SELECTED( wxID_ANY, PluginsManagerDialog::OnItemSelected )
        EVT_BUTTON( ID_ENABLE, PluginsManagerDialog::OnEnable )
        EVT_BUTTON( ID_DISABLE, PluginsManagerDialog::OnDisable )
    END_EVENT_TABLE()

    PluginsManagerDialog::PluginsManagerDialog( wxWindow *parent )
        : wxDialog( parent, wxID_ANY, wxT( "Plugins Manager" ), wxDefaultPosition, wxSize( 550, 350 ) )
    {
        wxBoxSizer *bsmain = new wxBoxSizer( wxHORIZONTAL );
        wxBoxSizer *l_boxsizer = new wxBoxSizer( wxVERTICAL );

        l_boxsizer->Add( new wxStaticText( this, wxID_ANY, wxT( "installed plugins:" ) ), 0, wxEXPAND);
        l_boxsizer->AddSpacer( 5 );

        //wxListCtrl
        m_listCtrl = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_SINGLE_SEL | wxLC_REPORT );

        wxListItem l_col0;
        l_col0.SetId( 0 );
        l_col0.SetText( wxT("name") );
        l_col0.SetWidth( 150 );
        m_listCtrl->InsertColumn( 0, l_col0 );

        wxListItem l_col1;
        l_col1.SetId( 1 );
        l_col1.SetText( wxT("version") );
        l_col1.SetWidth( 100 );
        m_listCtrl->InsertColumn( 1, l_col1 );

        wxListItem l_col2;
        l_col2.SetId( 2 );
        l_col2.SetText( wxT("enabled") );
        l_col2.SetWidth( 100 );
        m_listCtrl->InsertColumn( 2, l_col2 );

        l_boxsizer->Add( m_listCtrl, 1, wxEXPAND);
        l_boxsizer->AddSpacer( 5 );

    #ifdef USE_RICHTEXTCTRL
        m_richtextctrl = new wxRichTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( -1, 130 ), wxRE_READONLY | wxRE_MULTILINE );
        l_boxsizer->Add( m_richtextctrl, 0, wxEXPAND );
        m_richtextctrl->GetCaret()->Hide();
        m_richtextctrl->SetFont( wxFont( 10, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, _T( "Monospace" ) ) );
    #else
        m_textctrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( -1, 130 ), wxTE_READONLY | wxTE_MULTILINE );
        l_boxsizer->Add( m_textctrl, 0, wxEXPAND );
        //m_textctrl->GetCaret()->Hide();
        m_textctrl->SetFont( wxFont( 10, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, _T( "Monospace" ) ) );
    #endif // USE_RICHTEXTCTRL

        bsmain->Add( l_boxsizer, 1, wxEXPAND | wxALL, 5 );


        //buttons
        wxBoxSizer *l_bsbuttons = new wxBoxSizer( wxVERTICAL );
        wxButton *btnEnable = new wxButton( this, ID_ENABLE, wxT( "enable" ) );
        l_bsbuttons->AddSpacer( 20 );
        l_bsbuttons->Add( btnEnable, 0, wxEXPAND );
        l_bsbuttons->AddSpacer( 10 );
        wxButton *btnDisable = new wxButton( this, ID_DISABLE, wxT( "disable" ) );
        l_bsbuttons->Add( btnDisable, 0, wxEXPAND );

        bsmain->Add( l_bsbuttons, 0, wxEXPAND | wxALL, 5 );


        SetSizer( bsmain );


        LoadPlugins();
    }

    PluginsManagerDialog::~PluginsManagerDialog()
    {

    }

    void PluginsManagerDialog::LoadPlugins()
    {
        wxArrayString files;
        wxDir::GetAllFiles( sdk::Path::s_dirPlugins, &files, _T( "*.plg" ), wxDIR_FILES );

        for( size_t i=0; i<files.Count(); ++i )
        {
            FileConfig filecfg;
            if( filecfg.LoadFile( files[i] ) )
            {
                wxListItem item;
                item.SetId( i );
                m_listCtrl->InsertItem( item );

                m_listCtrl->SetItem( i, 0, files[i].AfterLast( _T( '/' ) ).BeforeLast( _T( '.' ) ) );
                m_listCtrl->SetItem( i, 1, filecfg.ReadString( _T( "/plugin" ), wxT( "unknown" ), _T( "version" ) ) );
                m_listCtrl->SetItem( i, 2, filecfg.ReadBool( _T( "/plugin" ), false, _T( "enabled" ) ) ? wxT( "yes" ) : wxT( "no" ) );
            }
        }
    }

    void PluginsManagerDialog::OnItemSelected( wxListEvent &evt )
    {
    #ifdef USE_RICHTEXTCTRL
        m_richtextctrl->Clear();

        FileConfig filecfg;

        if( filecfg.LoadFile( sdk::Path::s_dirPlugins + evt.GetText() + _T( ".plg" ) ) )
        {
            m_richtextctrl->BeginBold();
            m_richtextctrl->WriteText( evt.GetText() + _T( " " ) );
            m_richtextctrl->BeginTextColour( wxColour( _T( "#0000FF" ) ) );
            m_richtextctrl->WriteText( filecfg.ReadString( _T( "/plugin" ), wxT( "unknown" ), _T( "version" ) ) );
            m_richtextctrl->EndTextColour();
            m_richtextctrl->EndBold();

            m_richtextctrl->Newline();
            m_richtextctrl->Newline();

            m_richtextctrl->BeginItalic();
            m_richtextctrl->WriteText( sdk::Path::s_dirPlugins + filecfg.ReadString( _T( "/plugin" ), _T( "" ), _T( "library" ) ) );
            m_richtextctrl->EndItalic();

            m_richtextctrl->Newline();
            m_richtextctrl->Newline();

            m_richtextctrl->WriteText( filecfg.ReadString( _T( "/plugin" ), _T( "" ), _T( "description" ) ) );

            m_richtextctrl->Newline();
        }
    #else
        m_textctrl->SetValue( _T( "" ) );

        FileConfig filecfg;

        if( filecfg.LoadFile( sdk::Path::s_dirPlugins + evt.GetText() + _T( ".plg" ) ) )
        {

            *m_textctrl << evt.GetText() + _T( " " );
            *m_textctrl << filecfg.ReadString( _T( "/plugin" ), wxT( "unknown" ), _T( "version" ) );

            *m_textctrl <<  _T( "\n\n" );

            *m_textctrl << sdk::Path::s_dirPlugins + filecfg.ReadString( _T( "/plugin" ), _T( "" ), _T( "library" ) );

            *m_textctrl <<  _T( "\n\n" );

            *m_textctrl << filecfg.ReadString( _T( "/plugin" ), _T( "" ), _T( "description" ) );

            *m_textctrl <<  _T( "\n" );
        }
    #endif // USE_RICHTEXTCTRL
    }

    void PluginsManagerDialog::OnEnable(wxCommandEvent &evt )
    {
        const int item = m_listCtrl->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
        if( item != -1 )
        {
            const wxString &name = m_listCtrl->GetItemText( item );

            wxListItem row_info;
            row_info.m_itemId = item;
            row_info.m_col = 2;
            row_info.m_mask = wxLIST_MASK_TEXT;
            m_listCtrl->GetItem( row_info );
            const wxString &enabled = row_info.m_text;

            if( 0 != enabled.Cmp( wxT( "yes" ) ) )
            {
                FileConfig filecfg;

                if( filecfg.LoadFile( sdk::Path::s_dirPlugins + name + _T( ".plg" ) ) )
                {
                    filecfg.WriteBool( _T( "/plugin" ), true, _T( "enabled" ) );
                    filecfg.Save();
                    m_listCtrl->SetItem( item, 2, wxT( "yes" ) );
                }
            }
        }
    }

    void PluginsManagerDialog::OnDisable( wxCommandEvent &evt )
    {
        const int item = m_listCtrl->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
        if( item != -1 )
        {
            const wxString &name = m_listCtrl->GetItemText( item );

            wxListItem row_info;
            row_info.m_itemId = item;
            row_info.m_col = 2;
            row_info.m_mask = wxLIST_MASK_TEXT;
            m_listCtrl->GetItem( row_info );
            const wxString &enabled = row_info.m_text;

            if( 0 != enabled.Cmp( wxT( "no" ) ) )
            {
                FileConfig filecfg;

                if( filecfg.LoadFile( sdk::Path::s_dirPlugins + name + _T( ".plg" ) ) )
                {
                    filecfg.WriteBool( _T( "/plugin" ), false, _T( "enabled" ) );
                    filecfg.Save();
                    m_listCtrl->SetItem( item, 2, wxT( "no" ) );
                }
            }
        }
    }
}
